package com.zhiche.lisa.lspm.service.lsp.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.lspm.dao.mapper.lsp.LspLicenseAttachMapper;
import com.zhiche.lisa.lspm.dao.model.lsp.LspLicenseAttach;
import com.zhiche.lisa.lspm.dao.model.lsp.LspLicenseAttachHistory;
import com.zhiche.lisa.lspm.service.lsp.LspLicenseAttachHistoryService;
import com.zhiche.lisa.lspm.service.lsp.LspLicenseAttachService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 证照附件 service impl
 * @since 2018-05-24
 * @author lbl
 */
@Service
public class LspLicenseAttachServiceImpl extends ServiceImpl<LspLicenseAttachMapper, LspLicenseAttach> implements LspLicenseAttachService {
    @Autowired
    private LspLicenseAttachHistoryService lspLicenseAttachHistoryService;

    /**
     * 根据传入旧的附件信息进行删除
     * @param lspLicenseAttach
     */
    @Override
    @Transactional
    public void updateOldAttachDeleteState(LspLicenseAttach lspLicenseAttach){
        EntityWrapper<LspLicenseAttachHistory> ewAttach = new EntityWrapper<>();
        ewAttach.eq("lsp_id", lspLicenseAttach.getLspId());
        ewAttach.eq("license_id", lspLicenseAttach.getLicenseId());
        ewAttach.eq("attach_type", lspLicenseAttach.getAttachType());
        ewAttach.eq("attach_key", lspLicenseAttach.getAttachKey());
        List<LspLicenseAttachHistory> carrierLicenseAttaches = lspLicenseAttachHistoryService.selectList(ewAttach);
        if (CollectionUtils.isEmpty(carrierLicenseAttaches)) {
            LspLicenseAttachHistory lspLicenseAttachHistory = new LspLicenseAttachHistory();
            BeanUtils.copyProperties(lspLicenseAttach, lspLicenseAttachHistory);
            lspLicenseAttachHistory.setGmtCreate(new Date());
            lspLicenseAttachHistory.setCreator(AccountUtil.getAccountInfoFromSecurityContext().getUsername());
            lspLicenseAttachHistoryService.insert(lspLicenseAttachHistory);
        }
        this.deleteById(lspLicenseAttach.getId());
    }
}
